<template>
<div>
    <!--搜索栏-->
    <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item label="搜索：">
            <el-input size="small" v-model="formInline.materialName" placeholder="材料名称"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
        </el-form-item>
        <el-form-item>
            <el-button size="small" type="success" icon="el-icon-search" @click="handleAdd" v-if="userType == '0'">添加</el-button>
        </el-form-item>
    </el-form>

    <!--列表-->

    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
        <el-table-column sortable prop="materialName" label="材料名称">
            <template slot-scope="scope"><span :style="scope.row.amount <=  scope.row.warningAmount ? 'color: red;font-weight: bold;' : ''">{{scope.row.materialName}}</span></template>
        </el-table-column>
        <el-table-column sortable prop="amount" label="数量">
            <template slot-scope="scope"><span :style="scope.row.amount <=  scope.row.warningAmount ? 'color: red;font-weight: bold;' : ''">{{scope.row.amount}}</span></template>
        </el-table-column>
        <el-table-column sortable prop="unit" label="单位">
          <template slot-scope="scope"><span :style="scope.row.amount <=  scope.row.warningAmount ? 'color: red;font-weight: bold;' : ''">{{scope.row.unit}}</span>
          </template>
        </el-table-column>
        <el-table-column sortable prop="warningAmount" label="警戒值">
          <template slot-scope="scope"><span :style="scope.row.amount <=  scope.row.warningAmount ? 'color: red;font-weight: bold;' : ''">{{scope.row.warningAmount}}</span></template>
        </el-table-column>
        <el-table-column align="center" label="操作">
            <template slot-scope="scope">
                <el-button size="mini" type="success" @click="handleChange(scope.$index, scope.row)">变动</el-button>
                <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="mini" type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>

    <!-- 编辑界面，增加界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" @click="closeDialog">
        <el-form label-width="120px" :model="editForm" :rules="rules1" ref="editForm">
            <el-form-item label="材料名称" prop="materialName">
                <el-input v-model="editForm.materialName"></el-input>
            </el-form-item>
            <el-form-item label="数量" prop="amount">
                <el-input v-model="editForm.amount"></el-input>
            </el-form-item>
            <el-form-item label="单位" prop="unit">
                <el-input v-model="editForm.unit"></el-input>
            </el-form-item>
            <el-form-item label="警报数值" prop="warningAmount">
                <el-input v-model="editForm.warningAmount"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="closeDialog">取消</el-button>
            <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">提交</el-button>
        </div>
    </el-dialog>

    <!-- 变动界面 -->
    <el-dialog title="变动" :visible.sync="editFormVisibleChange" width="50%" @click="closeDialogChange">
        <el-form label-width="120px" :model="editFormChange" :rules="rules" ref="editFormChange">
            <el-form-item label="变动类型" prop="logType">
                <el-select v-model="editFormChange.logType" placeholder="请选择变动类型">
                    <el-option v-for="item in logTypes" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="变动数量" prop="amount">
                <el-input v-model="editFormChange.amount_change"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="editFormChange.remark" type="textarea" rows="3"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button size="small" @click="closeDialogChange">取消</el-button>
            <el-button size="small" type="primary" :loading="loading" class="title" @click="submitFormChange('editFormChange')">提交</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import * as api from '../../api/interface'
import Pagination from '../../components/Pagination'
export default {
    data() {
        return {
            loading: false, //是显示加载
            editFormVisible: false, //控制编辑弹窗显示与隐藏
            editFormVisibleChange: false, //控制变动弹窗显示与隐藏
            title: '',
            editForm: {},
            editFormChange: {},
            // rules表单验证--属性名与后端实体类属性名相同
            rules: {
                logType: [{ required: true, message: '请选择类型', trigger: 'blur' }],
                amount: [{ required: true, message: '请输入数量', trigger: 'blur' }],
            },
            rules1: {
                materialName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                amount: [{ required: true, message: '请输入数量', trigger: 'blur' }],
                unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
                warningAmount: [{ required: true, message: '请输入警戒数量', trigger: 'blur' }],
            },
            //查询条件
            formInline: {
                page: 1, //当前页面
                limit: 10, //每页数量
                materialName: ''
            },
            // 删除部门
            seletedata: {
                ids: '',
                token: localStorage.getItem('logintoken')
            },
            userparm: [], //搜索权限
            listData: [], //用户数据
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 10,
                total: 10
            },
            logTypes: [
                { value: "1", label: "入库" },
                { value: "2", label: "废弃" },
                { value: "3", label: "领用" }
            ],
            userType: ""
        }
    },
    // 注册组件
    components: {
        Pagination
    },
    /**
     * 页面初始化方法
     */
    created() {
        console.log(this.$store.state.userInfo)
        this.getData()
        this.userType = localStorage.getItem("userType");//获取已登录用户的用户类型
    },
    methods: {
        // 获取库存列表数据
        getData() {
            this.loading = true
            let para = {
                current: this.formInline.page,//当前页
                size: this.formInline.limit, //1页几条数据
                materialName: this.formInline.materialName //按材料名称搜索
            }
            api.queryMaterialByPage(para).then(data => {
                this.logining = false;
                if (data.message != "success") {
                    this.logining = false;
                    this.$message.error(data.message)
                } else {
                    this.loading = false
                    this.listData = data.returnPage.records
                    this.pageparm.currentPage = data.returnPage.current
                    this.pageparm.pageSize = data.returnPage.size
                    this.pageparm.total = data.returnPage.total
                }
            }).catch(e => {
                this.logining = false;
            });
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage
            this.formInline.limit = parm.pageSize
            this.getData()
        },
        //显示添加界面
        handleAdd: function (index, row) {
            this.editFormVisible = true
            this.title = '新增'
            this.editForm = {}
        },
        //显示编辑界面
        handleEdit: function (index, row) {
            this.editFormVisible = true
            this.title = '编辑'
            this.editForm = Object.assign({}, row);
        },
        //显示变动界面
        handleChange: function (index, row) {
            this.editFormVisibleChange = true
            row.amount_now = row.amount;
            row.amount_change = 0;
            this.editFormChange = Object.assign({}, row);
        },
        // 关闭编辑页面
        closeDialog() {
            this.editFormVisible = false
        },
        // 关闭变动页面
        closeDialogChange() {
            this.editFormVisibleChange = false
        },
        // 编辑、增加菜品信息
        submitForm(editData) {
            this.$refs[editData].validate(valid => {
                if (valid) {
                    let para = Object.assign({}, this.editForm);
                    this.loading = true;
                    api.updateMaterial(para)
                        .then(res => {
                            this.loading = false;
                            if (res.message == 'success') {
                                this.$message({ message: "提交成功", type: "success" });
                                this.getData();
                                this.editFormVisible = false;
                            } else {
                                this.$message({ type: 'error', message: res.message })
                            }
                        })
                        .catch(e => {
                            // this.editFormVisible = false
                            this.loading = false
                        })
                } else {
                    return false
                }
            })
        },
        // 变动保存方法
        submitFormChange(editData) {
            this.$refs[editData].validate(valid => {
                if (valid) {
                    if(this.editFormChange.logType == '3' && this.editFormChange.amount_now  < this.editFormChange.amount_change){
                      this.$message({message:"库存不足,领用失败",type:"error"});
                      return;
                    }
                    //后端需要的参数是amount
                    this.editFormChange.amount = this.editFormChange.amount_change;
                    let para = Object.assign({}, this.editFormChange);//获取变动的数值
                    para.createUser = localStorage.getItem("userId");//获取操作人
                    para.createUserName = localStorage.getItem("userName");
                    this.loading = true;
                    api.updateMaterialLog(para)
                        .then(res => {
                            this.loading = false;
                            if (res.message == 'success') {
                                this.$message({ message: "提交成功", type: "success" });
                                this.getData();

                                this.editFormVisibleChange = false;
                            } else {
                                this.$message({ type: 'error', message: res.message })
                            }
                        })
                        .catch(e => {
                            this.loading = false
                        })
                } else {
                    return false
                }
            })
        },
        // 删除
        handleDel(index, row) {
            this.$confirm('确定要删除吗?', '信息', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
                .then(() => {
                    let para = {
                        materialId: row.materialId
                    }
                    api.delMaterial(para)
                        .then(res => {
                            if (res.message == 'success') {
                                this.$message({ message: "删除成功", type: "success" });
                                this.getData();
                            } else {
                                this.$message({ type: 'error', message: res.message })
                            }
                        })
                        .catch(e => {
                            this.loading = false
                        })
                });
        }
    }
}
</script>

<style scoped>
.user-search {
    margin-top: 20px;
}
</style>
